from flask import Blueprint, request, render_template, session, redirect, make_response, flash
from models import *
import math

blue = Blueprint ("blue", __name__)


# =====================================================初始化数据表=======================================================

@blue.route ("/init")
def init ():
    db.create_all ()
    return "数据表创建成功"


# =====================================================添加商品==========================================================
@blue.route ("/addgoods", methods = ["GET", "POST"])
def addgoods ():
    cates = Cate.query.all ()
    goods = Goods.query.all ()
    if request.method == "GET":
        return render_template ("addgoods.html", cates = cates)
    name = request.form.get ("name")
    price = request.form.get ("price")
    cate = request.form.get ("cate")
    pic = request.files.get ("pic")
    if Goods.query.filter (Goods.name == name).all():
        flash ("该分类已存在，请重新添加。。。。。。")
        return redirect ("/addgoods")
    pic.save ("static/" + pic.filename)
    goods = Goods (name = name, price = price, cate_id = cate, photo = "../static/" + pic.filename)
    db.session.add (goods)
    db.session.commit ()
    flash("商品添加成功。。。。。。")
    return redirect ("/goodslist")


# =====================================================分页展示商品列表====================================================

@blue.route ("/goodslist")
def goodslist ():
    page_num = int (request.args.get ("page_num", 1))
    ttl = Goods.query.count ()
    per_page = 3
    per_ttl = math.ceil (ttl / per_page)
    pagination = Goods.query.paginate (page_num, per_page)
    cates = Cate.query.all ()
    if page_num <= 1:
        pev_page = 1
    else:
        pev_page = page_num - 1
    if page_num >= per_ttl:
        mext_num = per_ttl
    else:
        mext_num = page_num + 1

    return render_template ("goodslist.html", pag = pagination, cates = cates, pev_page = pev_page, mext_num = mext_num,
                            per_page = per_page, page_num = page_num)


# =====================================================修改数据==========================================================

@blue.route ("/updategoods/<int:id>", methods = ["GET", "POST"])
def updategoods (id):
    id = Goods.query.get (id)
    if request.method == "GET":
        cates = Cate.query.all ()
        return render_template ("updategoods.html", id = id, cates = cates)
    else:
        name = request.form.get ("name")
        price = request.form.get ("price")
        cate = request.form.get ("cate")
        photo = request.files.get ("pic")
        photo.save ("static/" + photo.filename)
        id.name = name
        id.price = price
        id.cate_id = cate
        id.photo = "../static/" + photo.filename
        db.session.commit ()
        flash("商品修改成功。。。。。。")
        return redirect ("/goodslist")


# =====================================================删除数据==========================================================

@blue.route ("/deletegoods/<int:id>", methods = ["GET", "POST"])
def deletegoods (id):
    id = Goods.query.get (id)
    db.session.delete (id)
    db.session.commit ()
    flash("商品删除成功。。。。。。")
    return redirect ("/goodslist")


# =====================================================详情页============================================================

@blue.route ("/parti/<int:id>")
def parti (id):
    cate = Goods.query.get (id)
    cate.count += 1
    db.session.commit ()
    return render_template ("parti.html", cate = cate)
